package io.osw.modules.sys.service.impl;

import io.osw.common.utils.Constant;
import io.osw.modules.sys.entity.SysUserEntity;
import io.osw.modules.sys.entity.TblCompanyEntity;
import io.osw.modules.sys.entity.TblSchoolEntity;
import io.osw.modules.sys.service.TblCompanyService;
import io.osw.modules.sys.service.TblSchoolService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.osw.common.utils.PageUtils;
import io.osw.common.utils.Query;

import io.osw.modules.sys.dao.TblSubinventoryDao;
import io.osw.modules.sys.entity.TblSubinventoryEntity;
import io.osw.modules.sys.service.TblSubinventoryService;


@Service("tblSubinventoryService")
public class TblSubinventoryServiceImpl extends ServiceImpl<TblSubinventoryDao, TblSubinventoryEntity> implements TblSubinventoryService {

    @Autowired
    private TblCompanyService tblCompanyService;

    @Autowired
    private TblSchoolService tblSchoolService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String subCode = (String)params.get("subCode");

        Page<TblSubinventoryEntity> page = this.selectPage(
                new Query<TblSubinventoryEntity>(params).getPage(),
                new EntityWrapper<TblSubinventoryEntity>()
                        .like(StringUtils.isNotBlank(subCode),"sub_code", subCode)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );


        List<TblSubinventoryEntity> records = page.getRecords();

        for (TblSubinventoryEntity record : records) {
            if (record == null) {
                return null;
            }

            //注入公司名
            TblCompanyEntity tblCompanyEntity = tblCompanyService.selectById(record.getCompanyId());
            record.setCompanyName(tblCompanyEntity.getCompanyName());

            //注入学校名
            TblSchoolEntity tblSchoolEntity = tblSchoolService.selectById(record.getSchoolId());
            record.setSchoolName(tblSchoolEntity.getSchoolName());

        }
        return new PageUtils(page);
    }

}
